package com.blb.exam.common.util;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 系统日志管理切面
 */
@Aspect
@Component
public class ServiceLogAspect {

    //获得日志输出对象
    private Logger logger = Logger.getLogger(ServiceLogAspect.class);

    @Around("execution(* com.blb.exam..*ServiceImpl.*(..))")
    public Object printLog(ProceedingJoinPoint joinPoint){
        if(logger.isDebugEnabled()){
            logger.debug("当前执行的方法：" + joinPoint.getSignature().getName());
        }
        Object[] args = joinPoint.getArgs();
        for(Object arg : args){
            if(logger.isDebugEnabled()){
                logger.debug("参数：" + arg);
            }
        }
        long start = System.currentTimeMillis();
        Object result = null;
        try {
            //执行原有方法
            result = joinPoint.proceed();
            if(logger.isDebugEnabled()){
                logger.debug("执行返回值：" + result);
            }
        } catch (Throwable throwable) {
            logger.error("出现异常",throwable);
        }
        long end = System.currentTimeMillis();
        if(logger.isDebugEnabled()){
            logger.debug("方法执行结束，执行时间是：" + (end - start));
        }
        return result;
    }
}
